/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年3月18日         Root.Lu
 */

package com.coocaa.app.gamecenter.pages.detail;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.coocaa.x.framework.utils.AsyncTask;
import com.coocaa.x.framework.utils.UIUtils;
import com.coocaa.x.uipackage.widget.SlideFocusView;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.data.SkyAppDetailData.AppAction;
import com.tianci.appstore.ui.data.SkyAppDetailData.AppStatus;
import com.tianci.appstore.utils.SkyConstants;

public class DownloadCtrView extends FrameLayout implements SlideFocusView.ISlideFocusView, OnClickListener {
    public static interface DownloadCtrViewListener {
        void onAction(AppAction action, Object obj);
    }

    private static final int _progressbar_color = Color.rgb(0x6f, 0xb3, 0x1e);
    private static final int _progressbar_height = SkyConstants.Div(14);
    private static final int _progressbar_width = SkyConstants.Div(256);// SkyConstants.Div(454);
    private static final int _linear_buttons_margin_top = SkyConstants.Div(36);
    private static final int _button_spacing = SkyConstants.Div(20);// SkyConstants.Div(56);
    private static final int _button_width = SkyConstants.Div(118);// SkyConstants.Div(196);
    private static final int _button_full_width = SkyConstants.Div(256);
    private static final int _button_height = SkyConstants.Div(60);
    private static final int _button_text_size = SkyConstants.Dpi(32);

    private AppStatus status = AppStatus.NOT_DOWNLOADED;
    private ProgressBar progressBar;
    private LinearLayout linearRoot, linearProgress, linearButtons;
    private Button button1, button2;
    private Object obj;
    private DownloadCtrViewListener listener;

    public DownloadCtrView(Context context) {
        super(context);
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.gamecenter_downloadctr, this);
        initView();
    }

    private void initView() {
        linearRoot = (LinearLayout) findViewById(R.id.linear_dlctr_root);
        linearProgress = (LinearLayout) findViewById(R.id.linear_dlctr_progressbar);
        UIUtils.setViewSize(linearProgress, _progressbar_width, _progressbar_height);
        progressBar = (ProgressBar) findViewById(R.id.progress_dlctr);
        progressBar.setBackgroundResource(R.drawable.gc_progressbar_bg);
        progressBar.setProgressDrawable(new ClipDrawable(new ColorDrawable(_progressbar_color),
                Gravity.LEFT, ClipDrawable.HORIZONTAL));
        progressBar.setMax(100);
        progressBar.setProgress(0);
        UIUtils.setViewSize(progressBar, _progressbar_width, _progressbar_height);
        // UIUtils.setLayoutParams(progressBar, linearRoot, -1, -1, _progressbar_width,
        // _progressbar_height, -1, -1, -1, -1);

        linearButtons = (LinearLayout) findViewById(R.id.linear_dlctr_buttons);
        UIUtils.setViewMargin(linearButtons, -1, _linear_buttons_margin_top, -1, -1);
        button1 = (Button) findViewById(R.id.btn_dlctr_btn1);
        button1.setTextColor(Color.WHITE);
        button1.setTextSize(_button_text_size);
        button1.setBackgroundColor(Color.rgb(52, 81, 114));
        button1.setFocusable(true);
        button1.setFocusableInTouchMode(true);
        button1.setPadding(0, 0, 0, 0);
        button1.setOnClickListener(this);

        button2 = (Button) findViewById(R.id.btn_dlctr_btn2);
        button2.setTextColor(Color.WHITE);
        button2.setTextSize(_button_text_size);
        button2.setBackgroundColor(Color.rgb(52, 81, 114));
        button2.setFocusable(true);
        button2.setFocusableInTouchMode(true);
        button2.setPadding(0, 0, 0, 0);
        button2.setOnClickListener(this);
        UIUtils.setViewMargin(button2, _button_spacing, -1, -1, -1);
        UIUtils.setViewSize(button1, _button_width, _button_height);
        UIUtils.setViewSize(button2, _button_width, _button_height);
        // UIUtils.setLayoutParams(button1, linearButtons, -1, -1, _button_width, _button_height,
        // -1,
        // -1, -1, -1);
        // UIUtils.setLayoutParams(button2, linearButtons, -1, -1, _button_width, _button_height,
        // _button_spacing, -1, -1, -1);
    }

    public static int Div(int x) {
        return SkyConstants.Div(x);
    }

    @Override
    public void initSlideFocusView(SlideFocusView parent) {
        parent.focusChangedEvent.registerView(button1, new SlideFocusView.FocusViewRevision(Div(30), Div(80)));
        parent.focusChangedEvent.registerView(button2, new SlideFocusView.FocusViewRevision(Div(30), Div(80)));
    }

    public void setDownloadCtrViewListener(DownloadCtrViewListener l) {
        listener = l;
    }

    public void updateStatus(AppStatus status, Object obj) {
        this.obj = obj;
        this.status = status;
        update(status);
    }

    public void updateDownloadPercent(int percent) {
        if (status == AppStatus.DOWNLOADING || status == AppStatus.DOWNLOAD_PAUSED) {
            progressBar.setProgress(percent);
        }
    }

    private void update(AppStatus status) {
        boolean bFocus = false;
        switch (status) {
            case NOT_DOWNLOADED:
                progressBar.setVisibility(View.GONE);
                bFocus = button1.isFocused();
                button1.setVisibility(View.GONE);
                button2.setVisibility(View.VISIBLE);
                UIUtils.setViewSize(button2, _button_full_width, _button_height);
                if (!bFocus)
                    bFocus = button2.isFocused();
                button2.setTag(AppAction.ACTION_DOWNLOAD);
                button2.setText(R.string.detail_download);
                if (bFocus) {
                    AsyncTask.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            button2.setVisibility(View.GONE);
                            button2.setVisibility(View.VISIBLE);
                            button2.requestFocus();
                        }
                    }, 200);
                }
                break;
            case DOWNLOADING:
                progressBar.setVisibility(View.VISIBLE);
                button1.setVisibility(View.VISIBLE);
                UIUtils.setViewSize(button1, _button_width, _button_height);
                button1.setTag(AppAction.ACTION_PAUSE);
                button1.setText(R.string.detail_pause);
                button2.setVisibility(View.VISIBLE);
                UIUtils.setViewSize(button2, _button_width, _button_height);
                button2.setTag(AppAction.ACTION_CANCEL);
                button2.setText(R.string.detail_cancle);
                //进入“下载”状态之后焦点默认落在“暂停”按钮上
                button1.requestFocus();
                if (button1.isFocused())
                    AsyncTask.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            button1.setVisibility(View.GONE);
                            button1.setVisibility(View.VISIBLE);
                            button1.requestFocus();
                        }
                    }, 200);
                else if (button2.isFocused())
                    AsyncTask.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            button2.setVisibility(View.GONE);
                            button2.setVisibility(View.VISIBLE);
                            button2.requestFocus();
                        }
                    }, 200);
                break;
            case DOWNLOADED:
                progressBar.setVisibility(View.GONE);
                bFocus = button1.isFocused();
                button1.setVisibility(View.GONE);
                button2.setVisibility(View.VISIBLE);
                if (!bFocus)
                    bFocus = button2.isFocused();
                UIUtils.setViewSize(button2, _button_full_width, _button_height);
                button2.setTag(AppAction.ACTION_INSTALL);
                button2.setText(R.string.install);
                if (bFocus) {
                    AsyncTask.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            button2.setVisibility(View.GONE);
                            button2.setVisibility(View.VISIBLE);
                            button2.requestFocus();
                        }
                    }, 200);
                }
                break;
            case INSTALLED_WITH_CAN_NOT_BE_UNINSTALLED:
            case INSTALLED:
                progressBar.setVisibility(View.GONE);
                bFocus = button1.isFocused();
                button1.setVisibility(View.GONE);
                button2.setVisibility(View.VISIBLE);
                if (!bFocus)
                    bFocus = button2.isFocused();
                UIUtils.setViewSize(button2, _button_full_width, _button_height);
                button2.setTag(AppAction.ACTION_LAUNCH);
                button2.setText(R.string.launch);
                if (bFocus)
                    AsyncTask.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            button2.setVisibility(View.GONE);
                            button2.setVisibility(View.VISIBLE);
                            button2.requestFocus();
                        }
                    }, 200);
                break;
            case DOWNLOAD_PAUSED:
                progressBar.setVisibility(View.VISIBLE);
                button1.setVisibility(View.VISIBLE);
                UIUtils.setViewSize(button1, _button_width, _button_height);
                button1.setTag(AppAction.ACTION_CONTINUE);
                button1.setText(R.string.detail_contiue_download);
                button2.setVisibility(View.VISIBLE);
                UIUtils.setViewSize(button2, _button_width, _button_height);
                button2.setTag(AppAction.ACTION_CANCEL);
                button2.setText(R.string.detail_cancle);
                if (button1.isFocused())
                    AsyncTask.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            button1.setVisibility(View.GONE);
                            button1.setVisibility(View.VISIBLE);
                            button1.requestFocus();
                        }
                    }, 200);
                else if (button2.isFocused())
                    AsyncTask.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            button2.setVisibility(View.GONE);
                            button2.setVisibility(View.VISIBLE);
                            button2.requestFocus();
                        }
                    }, 200);
                break;
            case HAS_NEW_VERSION_WITH_CAN_NOT_BE_UNINSTALLED:
            case HAS_NEW_VERSION:
                progressBar.setVisibility(View.GONE);
                button1.setVisibility(View.VISIBLE);
                UIUtils.setViewSize(button1, _button_width, _button_height);
                button1.setTag(AppAction.ACTION_UPGRADE);
                button1.setText(R.string.detail_upgrade);
                button2.setVisibility(View.VISIBLE);
                UIUtils.setViewSize(button2, _button_width, _button_height);
                button2.setTag(AppAction.ACTION_LAUNCH);
                button2.setText(R.string.launch);
                if (button1.isFocused())
                    AsyncTask.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            button1.setVisibility(View.GONE);
                            button1.setVisibility(View.VISIBLE);
                            button1.requestFocus();
                        }
                    }, 200);
                else if (button2.isFocused())
                    AsyncTask.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            button2.setVisibility(View.GONE);
                            button2.setVisibility(View.VISIBLE);
                            button2.requestFocus();
                        }
                    }, 200);
                break;
            case WAITING:
                progressBar.setVisibility(View.GONE);
                bFocus = button1.isFocused();
                button1.setVisibility(View.GONE);
                button2.setVisibility(View.VISIBLE);
                if (!bFocus)
                    bFocus = button2.isFocused();
                UIUtils.setViewSize(button2, _button_full_width, _button_height);
                button2.setTag(AppAction.ACTION_NULL);
                button2.setText(R.string.waiting);
                if (bFocus) {
                    AsyncTask.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            button2.setVisibility(View.GONE);
                            button2.setVisibility(View.VISIBLE);
                            button2.requestFocus();
                        }
                    }, 200);
                }
                break;
            case INSTALLING:
                progressBar.setVisibility(View.GONE);
                bFocus = button1.hasFocus();
                button1.setVisibility(View.GONE);
                button2.setVisibility(View.VISIBLE);
                if (!bFocus)
                    bFocus = button2.isFocused();
                UIUtils.setViewSize(button2, _button_full_width, _button_height);
                button2.setTag(AppAction.ACTION_NULL);
                button2.setText(R.string.installing);
                if (bFocus) {
                    AsyncTask.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            button2.setVisibility(View.GONE);
                            button2.setVisibility(View.VISIBLE);
                            button2.requestFocus();
                        }
                    }, 200);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(final View v) {
        AsyncTask.runOnThread(new Runnable() {
            public void run() {
                AppAction action = (AppAction) v.getTag();
                switch (action) {
                    case ACTION_DOWNLOAD:
                        break;
                    case ACTION_CANCEL:
                        break;
                    case ACTION_CONTINUE:
                        break;
                    case ACTION_DELETE:
                        break;
                    case ACTION_UPGRADE:
                        break;
                    case ACTION_INSTALL:
                        break;
                    case ACTION_LAUNCH:
                        break;
                    case ACTION_PAUSE:
                        break;
                    default:
                        return;
                }
                if (listener != null)
                    listener.onAction(action, obj);
            }
        });
    }

    public void setFocus() {
        if (button2.getVisibility() == View.VISIBLE)
            button2.requestFocus();
        else
            button1.requestFocus();
        //进入“下载”状态之后焦点默认落在“暂停”按钮上
        if(status == AppStatus.DOWNLOAD_PAUSED || status == AppStatus.DOWNLOADING)
            button1.requestFocus();
    }
}
